<template>
  <div>
    <!--    展期配置-->
    <el-row style="text-align: right">
      <el-button type="primary" @click="addExhibition" v-show="panePermiss[137]">{{ $t('新增') }}</el-button>
    </el-row>
    <el-row style="margin-top: 10px">
      <Table :page="page" row-key="id" :table-data="tableData" :table-column="tableColumn"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <el-table-column :label="$t('展期天数')" prop="rollOverDay" align="center"></el-table-column>
        <el-table-column :label="$t('展期利率')" prop="rollOverInterest" align="center">
          <template slot-scope="{row}">
            {{ row.rollOverInterest }}%
          </template>
        </el-table-column>
        <el-table-column :label="$t('创建时间')" prop="createTime" align="center"></el-table-column>
        <el-table-column :label="$t('更新时间')" prop="updateTime" align="center"></el-table-column>
        <template slot="operate">
          <el-table-column :label="$t('状态')" align="center">
            <template slot-scope="scope">
              <el-switch @change="changeStatus(scope.row)"
                         :disabled="panePermiss[138] ? false : true"
                         :value="scope.row.status"></el-switch>
            </template>
          </el-table-column>
          <el-table-column :label="$t('操作')" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="editExhibition(scope.row)" v-show="panePermiss[139]">{{
                  $t('编辑')
                }}
              </el-button>
            </template>
          </el-table-column>
        </template>
      </Table>
    </el-row>
    <Dialog :visible.sync="visible.isShow" @close="clearForm" :title="visible.title" @cancel="visible.isShow = false"
            @confirm="visibleConfirm" :confirm-loading="confirmLoading">
      <el-form :model="form" ref="form" label-width="120px" :rules="rulesForm">
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item :label="$t('展期天数')" prop="rollOverDay">
              <el-input v-model.number="form.rollOverDay">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('展期利率')" prop="rollOverInterest">
              <el-input v-model="form.rollOverInterest" oninput="value = value.replace(/[^\d.]/g, '')">
                <template slot="append">
                  %
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </Dialog>
  </div>
</template>

<script>
import {dateFormat, deepClone} from "@/utils/tools";
import {mapGetters} from 'vuex'
import {rollOverList, updateRollOverStatus, addRollOver, updateRollOver} from "@/api/product";

export default {
  props: ['panePermiss'],
  data() {
    const validatorLoanAmount = (rules, value, callBack) => {
      if (value < 1) {
        return callBack(new Error(this.$t('不能小于一天')));
      }
      return callBack();
    }
    const validatorRollOverInterest = (rules, value, callBack) => {
      if (parseInt(value) > 100) {
        return callBack(new Error(this.$t('利率不能超出100%')));
      }
      return callBack();
    }
    const validatorNumSize = (rules, value, callBack) => {
      const num = parseFloat(value)
      if (num < 1) {
        callBack(new Error(this.$t('必须输入大于等于1的数值')));
      }
      callBack();
    }
    return {
      tableData: [],
      tableColumn: [],
      form: {},
      rulesForm: {
        rollOverDay: [
          {required: true, message: this.$t('不能为空')},
          {required: true, validator: validatorLoanAmount}
        ],
        rollOverInterest: [
          {required: true, message: this.$t('不能为空')},
          {required: true, validator: validatorRollOverInterest},
          {required: true, validator: validatorNumSize},
        ],
      },
      visible: {
        isShow: false,
        type: '',
        title: '',
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      confirmLoading: false
    }
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const params = {
        ...this.page,
        productId: this.$route.query.productId
      }
      const res = await rollOverList(params)
      const {list, total} = res.data
      if (res.code === 1) {
        this.tableData = list.map(item => {
          item.status = item.status === 1
          return item
        })
        this.page = Object.assign({}, this.page, {
          total: total
        })
      }
    },
    clearForm() {
      this.form = {
        refundWay: 1,
        termType: 1
      }
      this.$refs.form.resetFields()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    async changeStatus(row) {
      const res = await updateRollOverStatus({
        id: row.id,
        status: row.status ? 0 : 1,
        productId: this.$route.query.productId,
        productName: this.$route.query.productName,
        rollOverDay: row.rollOverDay
      })
      if (res.code === 1) {
        this.$message.success(this.$t('修改成功'))
        this.getTableData()
      }
    },
    addExhibition() {
      this.visible = {
        isShow: true,
        type: 'add',
        title: this.$t('新增展期')
      }
    },
    editExhibition(row) {
      this.form = deepClone(row)
      this.visible = {
        isShow: true,
        type: 'edit',
        title: this.$t('编辑展期')
      }
    },
    async visibleConfirm() {
      this.$refs.form.validate(async v => {
        if (v) {
          let message = ''
          let api
          this.confirmLoading = true
          const params = {
            id: this.form.id,
            rollOverDay: this.form.rollOverDay,
            productId: this.$route.query.productId,
            productName: this.$route.query.productName,
            rollOverInterest: parseFloat(this.form.rollOverInterest),
          }
          if (this.visible.type === 'add') {
            message = this.$t('新增成功')
            api = addRollOver(params)
          } else {
            message = this.$t('修改成功')
            api = updateRollOver(params)
          }
          const res = await api
          if (res.code === 1) {
            this.$message.success(message)
            this.confirmLoading = false
            this.clearForm()
            this.visible.isShow = false
            this.getTableData()
          } else {
            this.confirmLoading = false
          }
        }
      })
    },

  }
}
</script>

<style scoped>

</style>
